import axios from "axios";
import router from "../router";
import elementUI from "element-ui";
let CancelToken = axios.CancelToken;
let http = axios.create({
  baseURL: "/api",
  withCredentials: true,
  timeout: 1000 * 10,
});
http.interceptors.request.use(function (config) {
  let rowPaths = ["/users/getCaptcha", "/users/login", "/users/verifyCaptcha"];
  if (rowPaths.includes(config.url)) {
    return config;
  } else {
    let token = localStorage.getItem("qf-token") || "";
    config.headers["authorization"] = token;
    return config;
  }
});
http.interceptors.response.use(function (config) {
  if (config.data.code === "10022" || config.data.code === "1004") {
    router.push("/login");
    localStorage.removeItem("qf-token");
    location.reload();
    elementUI.Message.error("登入过期，请重新登入");
  }
  return config;
});
export { http as default, CancelToken };
